package com.mjkj.demo.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * 考试随即试卷生成工具类
 *
 * @author cwh
 * @date 2023/2/13
 **/
public class ExaminationRandomUtils {


    public static <T,tClass> List<tClass> randomExamination(List<tClass> list, int count,Class<T> tClass) {

        List<tClass> newList = new ArrayList<>();
        int[] intRandom = new int[count];
        List<Integer> mist = new ArrayList<>();
        Random rd = new Random();
        while (mist.size() < count) {
            int num = rd.nextInt(list.size());
            if (!mist.contains(num)) {
                mist.add(num);
            }
        }
        for (int i = 0; i < mist.size(); i++) {
            intRandom[i] = (Integer) (mist.get(i));
        }
        for (int i = 0; i < intRandom.length; i++) {
            newList.add(list.get(intRandom[i]));
        }
        return newList;
    }

}
